<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/25
 * Time: 14:17
 */

namespace app\sports_admin\model;


use think\Model;
use think\Request;

class Help extends Model
{
    protected $table = "help";
    protected $pk    = "help_id";
    //关联教师表
    public function getTeacher()
    {
        return $this->belongsTo("Teacher","teacher_id","id");
    }
    /**展示问题所有的信息
     * @param $id id
     * @return mixed
     */
    public static function detail($id)
    {
        return self::with("getTeacher,getTeacher.school")->where('help_id',$id)->find();
    }
    /**
     * @param $page 页数
     * @param $rows 行数
     * @param $sort 排序字段
     * @param $sortOrder 排序规则
     * @param $searchName 关键字
     * @return \think\response\Json
     * 列表展示所有的问题
     */
    public static function answerList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $result = self::with('getTeacher')
            ->where(function($query)use($searchName){
            if($searchName){
                $query->where('help_title','like',"%{$searchName}%");}})
            ->order("{$sort}","{$sortOrder}")
            ->page($page)
            ->limit($rows)
            ->select();

        $count = self::with('getTeacher')
            ->where(function($query)use($searchName){
                if($searchName){
                $query->where('help_title','like',"%{$searchName}%");}
            })
            ->count();
            return json(['total'=>$count,'rows'=>$result]);
    }
    //回复法律援助问题
    public static function updateQuestion()
    {
        $data = Request::instance()->only('id,answer,question,title');
        $answer = htmlspecialchars($data['answer']);
        $time = time();
        return self::where('help_id',$data['id'])
            ->update([
                "help_answer"=>$answer,
                "update_time"=>$time,
                'help_question'=>$data['question'],
                'help_tittle'=>$data['title']]);
    }
    //添加援助问题
    public static function addQuestion()
    {
        $data = Request::instance()->only('answer,question,title');
        $answer = htmlspecialchars($data['answer']);
        $question= htmlspecialchars($data['question']);
        return self::create(['help_answer'=>$answer,'help_tittle'=>$data['title'],'help_question'=>$question]);
    }

}